import clang.cindex
parsingFileName=""
parsingFileData=[]
oldParsedFileData=""
def loadFileData(fName):
    global oldParsedFileData
    if fName==oldParsedFileData:
        return
    oldParsedFileData=fName
    global parsingFileData
    parsingFileData=[]
    for line in open(fName,'r').readlines():
        line=line.replace("\n","")
        putData=[]
        for x in line:
            putData.append(x)
        parsingFileData.append(putData)

def getTextFromSourceRange(ext):
    loadFileData(ext.start.file.name)
    start=ext.start
    end=ext.end
    txt=""
    for p in range(start.line-1,end.line):
        if p>=len(parsingFileData):
            return txt
        if txt!="":
            txt=txt+"\n"
        if p==start.line-1 and p==end.line-1:
            for c in range(start.column-1,end.column-1):
                if c<len(parsingFileData[p]):
                    txt=txt+parsingFileData[p][c]
        elif p==end.line-1:
            for c in range(0,end.column-1):
                if c<len(parsingFileData[p]):
                    txt=txt+parsingFileData[p][c]
        elif p==start.line-1:
            for c in range(start.column-1,len(parsingFileData[p])-1):
                if c<len(parsingFileData[p]):
                    txt=txt+parsingFileData[p][c]
        else:
            txt=txt+str(parsingFileData[p])
    return txt
def printCursor(c=clang.cindex.Cursor,tb=""):

        

    print tb+str(c.kind.name)
    print tb+str(c.spelling)
    #if c.kind.name=="CXX_BASE_SPECIFIER":
    #    print tb+str(c.get_cxx_access_specifier()).replace("CX_CXX","").lower()
    #    print c.type.get_declaration().get_completion_string()[0].spelling
    print tb+str(c.get_completion_string())
    if c.kind.name=="INTEGER_LITERAL":
        print tb+getTextFromSourceRange(c.extent)
        
    for x in c.get_children():
        printCursor(x,tb+"|----")


indx=clang.cindex.Index.create()
tu=indx.parse("C:\Users\Tomek\Desktop\Projekty\Visual Studio 11\DarkBolt Game Engine\Designer\include\irr\parsing2.cpp")
loadFileData("C:\Users\Tomek\Desktop\Projekty\Visual Studio 11\DarkBolt Game Engine\Designer\include\irr\parsing2.cpp")

printCursor(tu.cursor,"")

#print tu.cursor.spelling


print "---------------"

#print parsed.getPublic().data["fields"]

print "---------------"